package week2;

import week2.exceptions.EmptyCollectionException;

public class LinkedStack<T> {
    LinearNode<T> top;
    int count;
    public LinkedStack()
    {
        count=0;
        top=null;
    }
    public void push (T element)
    {
        LinearNode<T> temp= new LinearNode<>(element);

        temp.setNext(top);
        top=temp;
        count++;
    }
    public T pop()
    {
        T result =   top.getElement();
        top=top.getNext();
        count--;
        return result;
    }
    public int size()
    {
        return count;
    }
    public T peek() {
        if (isEmpty())
        {
            throw new EmptyCollectionException("Stack");
        }
        else
        {
            return top.getElement();
        }
    }
    public boolean isEmpty()
    {
        if (count==0){
            return true;
        }
        else
            return false;
    }
    public String toString()
    {
        LinearNode<T> temp;
        temp = top;
        String result = "";
        while(temp != null)
        {
             result += temp.getElement() + " ";
              temp = temp.getNext();
        }
        return result;
    }
}
